package co.yixiang.modules.shop.service.impl;

import co.yixiang.modules.shop.domain.YxSalesCommissionDetail;
import co.yixiang.modules.shop.repository.YxSalesCommissionDetailRepository;
import co.yixiang.modules.shop.service.YxSalesCommissionDetailService;
import co.yixiang.modules.shop.service.dto.YxSalesCommissionDetailDto;
import co.yixiang.modules.shop.service.dto.YxSalesCommissionDetailQueryCriteria;
import co.yixiang.modules.shop.service.mapper.YxSalesCommissionDetailMapper;
import co.yixiang.utils.ValidationUtil;
import co.yixiang.utils.FileUtil;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
// 默认不使用缓存
//import org.springframework.cache.annotation.CacheConfig;
//import org.springframework.cache.annotation.CacheEvict;
//import org.springframework.cache.annotation.Cacheable;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import co.yixiang.utils.PageUtil;
import co.yixiang.utils.QueryHelp;
import java.util.List;
import java.util.Map;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.LinkedHashMap;

/**
* @author frq
* @date 2020-06-19
*/
@Service
@Transactional(propagation = Propagation.SUPPORTS, readOnly = true, rollbackFor = Exception.class)
public class YxSalesCommissionDetailServiceImpl implements YxSalesCommissionDetailService {

    private final YxSalesCommissionDetailRepository yxSalesCommissionDetailRepository;

    private final YxSalesCommissionDetailMapper yxSalesCommissionDetailMapper;

    public YxSalesCommissionDetailServiceImpl(YxSalesCommissionDetailRepository yxSalesCommissionDetailRepository, YxSalesCommissionDetailMapper yxSalesCommissionDetailMapper) {
        this.yxSalesCommissionDetailRepository = yxSalesCommissionDetailRepository;
        this.yxSalesCommissionDetailMapper = yxSalesCommissionDetailMapper;
    }

    @Override
    //@Cacheable
    public Map<String,Object> queryAll(YxSalesCommissionDetailQueryCriteria criteria, Pageable pageable){
        Page<YxSalesCommissionDetail> page = yxSalesCommissionDetailRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder),pageable);
        return PageUtil.toPage(page.map(yxSalesCommissionDetailMapper::toDto));
    }

    @Override
    //@Cacheable
    public List<YxSalesCommissionDetailDto> queryAll(YxSalesCommissionDetailQueryCriteria criteria){
        return yxSalesCommissionDetailMapper.toDto(yxSalesCommissionDetailRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder)));
    }

    @Override
    //@Cacheable(key = "#p0")
    public YxSalesCommissionDetailDto findById(Integer id) {
        YxSalesCommissionDetail yxSalesCommissionDetail = yxSalesCommissionDetailRepository.findById(id).orElseGet(YxSalesCommissionDetail::new);
        ValidationUtil.isNull(yxSalesCommissionDetail.getId(),"YxSalesCommissionDetail","id",id);
        return yxSalesCommissionDetailMapper.toDto(yxSalesCommissionDetail);
    }

    @Override
    //@CacheEvict(allEntries = true)
    @Transactional(rollbackFor = Exception.class)
    public YxSalesCommissionDetailDto create(YxSalesCommissionDetail resources) {
        return yxSalesCommissionDetailMapper.toDto(yxSalesCommissionDetailRepository.save(resources));
    }

    @Override
    //@CacheEvict(allEntries = true)
    @Transactional(rollbackFor = Exception.class)
    public void update(YxSalesCommissionDetail resources) {
        YxSalesCommissionDetail yxSalesCommissionDetail = yxSalesCommissionDetailRepository.findById(resources.getId()).orElseGet(YxSalesCommissionDetail::new);
        ValidationUtil.isNull( yxSalesCommissionDetail.getId(),"YxSalesCommissionDetail","id",resources.getId());
        yxSalesCommissionDetail.copy(resources);
        yxSalesCommissionDetailRepository.save(yxSalesCommissionDetail);
    }

    @Override
    //@CacheEvict(allEntries = true)
    public void deleteAll(Integer[] ids) {
        for (Integer id : ids) {
            yxSalesCommissionDetailRepository.deleteById(id);
        }
    }


}